﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using System.IO;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Member
{
    public partial class UserMemberCardList : System.Web.UI.Page
    {

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            ParamKit.Bind("MemberCardKind", ddl_member_card_kind, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "", "");
            if (!string.IsNullOrEmpty(Request["kind"]))
            {
                ddl_member_card_kind.SelectedValue = Request["kind"];
            }
            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            BindAgentList(pageIndex, pageSize);
        }
        /// <summary>
        /// 绑定代理人数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">记录条数</param>
        private void BindAgentList(int pageIndex, int pageSize)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                QueryFilterList filters = new QueryFilterList();

                string fromSql = " from WxMemberCardRight r where 1=1 ";

                if (!string.IsNullOrEmpty(Request["ruleid"]))
                {
                    filters.Add(new StringFilter("r.RuleId", "RuleId", Request["ruleid"], StringCompairType.Like));
                }
                if (!string.IsNullOrEmpty(Request["cardno"] ))
                {
                    filters.Add(new StringFilter("r.CardNo", "CardNo", Request["cardno"], StringCompairType.Like));
                }
                if (!string.IsNullOrEmpty(Request["subcard"] ))
                {
                    filters.Add(new StringFilter("r.SubCardNo", "SubCardNo", Request["subcard"], StringCompairType.Like));
                }
                if (!string.IsNullOrEmpty( Request["kind"] )&&Request["kind"]!="-1")
                {
                    filters.Add(new StringFilter("r.Kind", "Kind", Request["kind"], StringCompairType.Like));
                }

                string condtion = "";

                foreach (DataFilter filter in filters)
                {
                    condtion += " and " + filter.GetSection();
                }

                //获取记录总数
                IQuery cntquery = session.CreateQuery("select count(*)" + fromSql + condtion);
                filters.SetUp(cntquery);

                _framePage.RecordCount = Convert.ToInt32(cntquery.UniqueResult());

                string select = " ";

                //获取数据
                IQuery dataquery = session.CreateQuery(select + fromSql + condtion);
                filters.SetPager(dataquery, pageIndex, pageSize);
                filters.SetUp(dataquery);

                repeater1.DataSource = dataquery.List<WxMemberCardRight>();
                repeater1.DataBind();
            }
        }
    }
}